package co.fitstart.weapp.api.web.controller.course;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.weapp.api.service.CourseAchievementService;
import co.fitstart.weapp.api.service.CourseRoundService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping("courses/{courseId}/course-achievement")
public class CourseAchievementController {

    private Logger logger = LoggerFactory.getLogger(CourseAchievementController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private CourseRoundService courseRoundService;

    @Autowired
    private CourseAchievementService courseAchievementService;

    /**
     * 
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult index(@PathVariable("courseId") Long courseRoundId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            CourseRound courseRound = courseRoundService.load(courseRoundId);
            
            httpResult.success().message("获取成就成功")
                                .data("courseAchievement", courseAchievementService.getCourseAchievement(courseRound));
            
        } catch (Exception e) {
            logger.error("courseAchievement() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
        
    }

    /**
     * 
     * @param unionid
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/share", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult share(@RequestParam("unionid") String unionid,
                            @PathVariable("courseId") Long courseRoundId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            Student student = userService.getStudent(user);
            CourseRound courseRound = courseRoundService.load(courseRoundId);
            
            courseRoundService.validate(student, courseRound);
            
            httpResult.success().message("获取我的成就分享成功")
                                .data("courseAchievementShare", courseAchievementService.getCourseAchievementShare(courseRound));
        } catch (BusinessException e) {
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("courseAchievementShare() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
        
    }

}
